﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Simulation.Framework
{
    public class StepManager : ManagerBehavior
    {
        private List<StepBase> stepList = new List<StepBase>();
        private int curSteIndex;

        #region override

        public override void Init()
        {
            stepList = new List<StepBase>();
        }

        public override void Clear()
        {
            ClearAllStep();
        }

        public override void Restar() { }

        #endregion

        public void AddStep(StepBase step)
        {
            if (stepList == null)
                stepList = new List<StepBase>();
            step.Init();
            stepList.Add(step);
        }

        public void RemoveStep(StepBase step)
        {
            if (stepList.Contains(step))
            {
                step.Stop();
                step.Clear();
                stepList.Remove(step);
                curSteIndex--;
            }
        }

        public void RemoveAll()
        {
            for (int i = 0; i < stepList.Count; i++)
            {
                StepBase step = stepList[i];
                step.Stop();
                step.Clear();
                stepList.Remove(step);
            }
            curSteIndex = 0;
        }

        public void PlayStep()
        {
            PlayStep(curSteIndex);
        }

        public void PlayStep(int index)
        {
            if (stepList == null || index >= stepList.Count || index < 0)
                return;
            StepBase step = stepList[index];
            PlayStep(step);
        }

        public void PlayStep(StepBase step)
        {
            if (stepList == null || curSteIndex >= stepList.Count || !stepList.Contains(step))
                return;
            StepBase curStep = stepList[curSteIndex];
            curStep.Stop();
            curSteIndex = stepList.IndexOf(step);
            step.Start();
        }

        public void Next()
        {
            if (stepList == null || stepList.Count <= curSteIndex + 1)
                return;
            PlayStep(curSteIndex + 1);
        }

        public void Previous()
        {
            if (0 == curSteIndex)
                return;
            PlayStep(curSteIndex - 1);
        }

        public void Pause()
        {
            if (stepList == null || curSteIndex >= stepList.Count)
                return;
            stepList[curSteIndex].Pause();
        }

        public void UnPause()
        {
            if (stepList == null || curSteIndex >= stepList.Count)
                return;
            stepList[curSteIndex].UnPause();
        }

        public void Stop()
        {
            if (stepList == null || curSteIndex >= stepList.Count)
                return;
            stepList[curSteIndex].Stop();
        }

        public StepBase GetCurStep() {
            if (stepList == null || stepList.Count <= curSteIndex || stepList.Count == 0)
                return null;
            return stepList[curSteIndex];
        }

        private void ClearAllStep()
        {
            if (stepList == null)
                return;
            for (int i = 0; i < stepList.Count; i++)
            {
                StepBase step = stepList[i];
                step.Stop();
                step.Clear();
                step = null;
                stepList.RemoveAt(i);
            }
            stepList.Clear();
            stepList = null;
        }
    }
}